<?php
namespace app\apiadmin\controller\doudian;


use app\common\controller\BaseController;
use app\common\testCoss;
use app\common\utils\curl;
use app\common\utils\Res;
use app\Request;
use think\facade\Cache;
use think\facade\Db;

class Sms extends BaseController
{

    /**
     * @author: oliver
     * @Description: 获取用户类型数组
     * @Date: 2024-05-08 15:41:00
     */

    private $appKey = '7372060210849990182';
    private $appSecret = '79440ea4-0cd8-4036-b820-d05b7db20076';
//    private $shop_id = '22277625';//卡券测试店铺
    private $shop_id = '165563937';//毛毛虫教育
    private $grant_type = 'authorization_self';
    private $sign_method = 'hmac-sha256';

    /**
     * @author: oliver
     * @Description: 抖店字符串排序接口
     * @Date: 2024-06-04 00:00:00
     */
    public  function rec_ksort(array &$arr) {
        $kstring = true;
        foreach ($arr as $k => &$v) {
            if (!is_string($k)) {
                $kstring = false;
            }
            if (is_array($v)) {
                $this->rec_ksort($v);
            }
        }
        if ($kstring) {
            ksort($arr);
        }
    }

    /**
     * @author: oliver
     * @Description: 抖店获取签名sign接口
     * @Date: 2024-06-04 00:00:00
     */
    public function get_sign($timestamp,$paramJson,$method)
    {
//        $a1 = array(
//            'code'=>'',
//            'grant_type'=>$this->grant_type,
//            'shop_id'=>$this->shop_id,
//        );
        $appKey = $this->appKey;
        $appSecret = $this->appSecret;

        $paramPattern = 'app_key' . $appKey . 'method' . $method . 'param_json' . $paramJson . 'timestamp' . $timestamp . 'v2';
        $signPattern = $appSecret . $paramPattern . $appSecret;
        $sign =  hash_hmac("sha256", $signPattern, $appSecret);

        $data['result'] =$sign;
        return $sign;
    }

    /**
     * @author: oliver
     * @Description: 抖店获取access_token接口
     * @Date: 2024-06-04 00:00:00
     */
    public function get_token($timestamp){
        $a1 = array(
            'code'=>'',
            'grant_type'=>$this->grant_type,
            'shop_id'=>$this->shop_id,
        );
        $paramJson =  $this->get_param_json($a1);

        $appKey = $this->appKey;
        $appSecret = $this->appSecret;
        $method = 'token.create';
        $v= '2';
        $sign = $this->get_sign($timestamp,$paramJson,$method);

        $url = 'https://openapi-fxg.jinritemai.com/token/create';

        $params = array(
            'app_key'=>$appKey,
            'method'=>$method,
            'param_json'=>$paramJson,
            'timestamp'=>$timestamp,
            'v'=>'2',
            'sign'=>$sign,
            'sign_method'=>'hmac-sha256',
        );
        $json_obj = Curl::callWebServer($url,$params,'POST',true,false);
        return $json_obj;
    }
    /**
     * @author: oliver
     * @Description: 抖店获取参数json公共方法接口
     * @Date: 2024-06-04 00:00:00
     */
    public function  get_param_json($a1){
        $this->rec_ksort($a1); // 对关联数组中的kv，执行排序，需要递归
        $s = json_encode($a1, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); // 重新序列化，确保所有key按字典序排序
        // 加入flag，确保斜杠不被escape，汉字不被escape
        return $s;
    }

    /**
     * @author: oliver
     * @Description: 抖店短信模板申请接口
     * @Date: 2024-06-04 00:00:00
     */
    public function sms_template_apply(){
        $appKey = $this->appKey;
        $appSecret = $this->appSecret;
        $timestamp =time();
        $v= '2';
        $token_info = $this->get_token($timestamp);
        $token = $token_info['data']['access_token'];

        $method = 'sms.template.apply';
        $url = 'https://openapi-fxg.jinritemai.com/sms/template/apply';

        $a1 = array(
            'code'=>'',
            'grant_type'=>$this->grant_type,
            'shop_id'=>$this->shop_id,
            //此处是接口特殊参数
            //参数样例
//            'sms_account'=>'7d49c08a',
//            'template_type'=>'CN_NTC',
//            'template_name'=>'下单成功',
//            'template_content'=>'您已成功下单李洁老师的课程，兑换码：${code}，点我提取课程视频：${url}',
            'sms_account'=>$_POST['sms_account'],
            'template_type'=>$_POST['template_type'],
            'template_name'=>$_POST['template_name'],
            'template_content'=>$_POST['template_content'],

        );
        $paramJson =  $this->get_param_json($a1);
        $sign = $this->get_sign($timestamp,$paramJson,$method);
        $params = array(
            'app_key'=>$appKey,
            'method'=>$method,
            'param_json'=>$paramJson,
            'timestamp'=>$timestamp,
            'v'=>'2',
            'sign'=>$sign,
            'sign_method'=> $this->sign_method,
            'access_token'=>$token,

        );
        $json_obj = Curl::callWebServer($url,$params,'POST',true,false);

        $data['paramJson'] = $paramJson;
        $data['sign'] = $sign;
        $data['token'] = $token;
        $data['result'] = $json_obj;
        return Res::ok($data);
    }

    /**
     * @author: oliver
     * @Description: 抖店短信模板列表接口
     * @Date: 2024-06-04 00:00:00
     */
    public function sms_template_list(){
        $appKey = $this->appKey;
        $appSecret = $this->appSecret;
        $timestamp =time();
        $v= '2';
        $token_info = $this->get_token($timestamp);
        $token = $token_info['data']['access_token'];

        $method = 'sms.template.apply.list';
        $url = 'https://openapi-fxg.jinritemai.com/sms/template/apply/list';

        $a1 = array(
            'code'=>'',
            'grant_type'=>$this->grant_type,
            'shop_id'=>$this->shop_id,
            //此处是接口特殊参数

            'sms_account'=>$_POST['sms_account'],

        );
        $paramJson =  $this->get_param_json($a1);
        $sign = $this->get_sign($timestamp,$paramJson,$method);
        $params = array(
            'app_key'=>$appKey,
            'method'=>$method,
            'param_json'=>$paramJson,
            'timestamp'=>$timestamp,
            'v'=>'2',
            'sign'=>$sign,
            'sign_method'=> $this->sign_method,
            'access_token'=>$token,

        );
        $json_obj = Curl::callWebServer($url,$params,'POST',true,false);

        $data['paramJson'] = $paramJson;
        $data['sign'] = $sign;
        $data['token'] = $token;
        $data['result'] = $json_obj;
        return Res::ok($data);
    }

    /**
     * @author: oliver
     * @Description: 抖店短信 签名申请 接口
     * @Date: 2024-06-04 00:00:00
     */
    public function sms_sign_apply(){
        $appKey = $this->appKey;
        $appSecret = $this->appSecret;
        $timestamp =time();
        $v= '2';
        $token_info = $this->get_token($timestamp);
        $token = $token_info['data']['access_token'];

        $method = 'sms.sign.apply';
        $url = 'https://openapi-fxg.jinritemai.com/sms/sign/apply';

        $a1 = array(
            'code'=>'',
            'grant_type'=>$this->grant_type,
            'shop_id'=>$this->shop_id,
            //此处是接口特殊参数

            'sms_account'=>$_POST['sms_account'],
            'sign'=>$_POST['sign'],

        );
        $paramJson =  $this->get_param_json($a1);
        $sign = $this->get_sign($timestamp,$paramJson,$method);
        $params = array(
            'app_key'=>$appKey,
            'method'=>$method,
            'param_json'=>$paramJson,
            'timestamp'=>$timestamp,
            'v'=>'2',
            'sign'=>$sign,
            'sign_method'=> $this->sign_method,
            'access_token'=>$token,

        );
        $json_obj = Curl::callWebServer($url,$params,'POST',true,false);

        $data['paramJson'] = $paramJson;
        $data['sign'] = $sign;
        $data['token'] = $token;
        $data['result'] = $json_obj;
        return Res::ok($data);
    }

    /**
     * @author: oliver
     * @Description: 抖店短信 签名申请 接口
     * @Date: 2024-06-04 00:00:00
     */
    public function sms_sign_apply_list(){
        $appKey = $this->appKey;
        $appSecret = $this->appSecret;
        $timestamp =time();
        $v= '2';
        $token_info = $this->get_token($timestamp);
        $token = $token_info['data']['access_token'];

        $method = 'sms.sign.apply.list';
        $url = 'https://openapi-fxg.jinritemai.com/sms/sign/apply/list';

        $a1 = array(
            'code'=>'',
            'grant_type'=>$this->grant_type,
            'shop_id'=>$this->shop_id,
            //此处是接口特殊参数

            'sms_account'=>$_POST['sms_account'],

        );
        $paramJson =  $this->get_param_json($a1);
        $sign = $this->get_sign($timestamp,$paramJson,$method);
        $params = array(
            'app_key'=>$appKey,
            'method'=>$method,
            'param_json'=>$paramJson,
            'timestamp'=>$timestamp,
            'v'=>'2',
            'sign'=>$sign,
            'sign_method'=> $this->sign_method,
            'access_token'=>$token,

        );
        $json_obj = Curl::callWebServer($url,$params,'POST',true,false);

        $data['paramJson'] = $paramJson;
        $data['sign'] = $sign;
        $data['token'] = $token;
        $data['result'] = $json_obj;
        return Res::ok($data);
    }


    /**
     * @author: oliver
     * @Description: 抖店短信 签名申请 接口
     * @Date: 2024-06-04 00:00:00
     */
    public function sms_send(){
        $appKey = $this->appKey;
        $appSecret = $this->appSecret;
        $timestamp =time();
        $v= '2';
        $token_info = $this->get_token($timestamp);
        $token = $token_info['data']['access_token'];

        $method = 'sms.send';
        $url = 'https://openapi-fxg.jinritemai.com/sms/send';

        $a1 = array(
            'code'=>'',
            'grant_type'=>$this->grant_type,
            'shop_id'=>$this->shop_id,
            //此处是接口特殊参数

            'sms_account'=>$_POST['sms_account'],
            'sign'=>$_POST['sign'],
            'template_id'=>$_POST['template_id'],
            'template_param'=>$_POST['template_param'],
            'post_tel'=>$_POST['post_tel'],

        );
        $paramJson =  $this->get_param_json($a1);
        $sign = $this->get_sign($timestamp,$paramJson,$method);
        $params = array(
            'app_key'=>$appKey,
            'method'=>$method,
            'param_json'=>$paramJson,
            'timestamp'=>$timestamp,
            'v'=>'2',
            'sign'=>$sign,
            'sign_method'=> $this->sign_method,
            'access_token'=>$token,

        );
        $json_obj = Curl::callWebServer($url,$params,'POST',true,false);

        $data['paramJson'] = $paramJson;
        $data['sign'] = $sign;
        $data['token'] = $token;
        $data['result'] = $json_obj;
        return Res::ok($data);
    }
    /**
     * @author: oliver
     * @Description: 抖店短信 签名删除 接口
     * @Date: 2024-06-04 00:00:00
     */
    public function sms_sign_del(){
        $appKey = $this->appKey;
        $appSecret = $this->appSecret;
        $timestamp =time();
        $v= '2';
        $token_info = $this->get_token($timestamp);
        $token = $token_info['data']['access_token'];

        $method = 'sms.sign.delete';
        $url = 'https://openapi-fxg.jinritemai.com/sms/sign/delete';

        $a1 = array(
            'code'=>'',
            'grant_type'=>$this->grant_type,
            'shop_id'=>$this->shop_id,
            //此处是接口特殊参数

            'sms_account'=>$_POST['sms_account'],
            'sign'=>$_POST['sign'],

        );
        $paramJson =  $this->get_param_json($a1);
        $sign = $this->get_sign($timestamp,$paramJson,$method);
        $params = array(
            'app_key'=>$appKey,
            'method'=>$method,
            'param_json'=>$paramJson,
            'timestamp'=>$timestamp,
            'v'=>'2',
            'sign'=>$sign,
            'sign_method'=> $this->sign_method,
            'access_token'=>$token,

        );
        $json_obj = Curl::callWebServer($url,$params,'POST',true,false);

        $data['paramJson'] = $paramJson;
        $data['sign'] = $sign;
        $data['token'] = $token;
        $data['result'] = $json_obj;
        return Res::ok($data);
    }
    /**
     * @author: oliver
     * @Description: 抖店短信 签名撤销 接口
     * @Date: 2024-06-04 00:00:00
     */
    public function sms_sign_revoke(){
        $appKey = $this->appKey;
        $appSecret = $this->appSecret;
        $timestamp =time();
        $v= '2';
        $token_info = $this->get_token($timestamp);
        $token = $token_info['data']['access_token'];

        $method = 'sms.sign.apply.revoke';
        $url = 'https://openapi-fxg.jinritemai.com/sms/sign/apply/revoke';

        $a1 = array(
            'code'=>'',
            'grant_type'=>$this->grant_type,
            'shop_id'=>$this->shop_id,
            //此处是接口特殊参数

            'sms_account'=>$_POST['sms_account'],
            'sms_sign_apply_id'=>$_POST['sms_sign_apply_id'],

        );
        $paramJson =  $this->get_param_json($a1);
        $sign = $this->get_sign($timestamp,$paramJson,$method);
        $params = array(
            'app_key'=>$appKey,
            'method'=>$method,
            'param_json'=>$paramJson,
            'timestamp'=>$timestamp,
            'v'=>'2',
            'sign'=>$sign,
            'sign_method'=> $this->sign_method,
            'access_token'=>$token,

        );
        $json_obj = Curl::callWebServer($url,$params,'POST',true,false);

        $data['paramJson'] = $paramJson;
        $data['sign'] = $sign;
        $data['token'] = $token;
        $data['result'] = $json_obj;
        return Res::ok($data);
    }
    /**
     * @author: oliver
     * @Description: 抖店短信 模板删除 接口
     * @Date: 2024-06-04 00:00:00
     */
    public function sms_template_del(){
        $appKey = $this->appKey;
        $appSecret = $this->appSecret;
        $timestamp =time();
        $v= '2';
        $token_info = $this->get_token($timestamp);
        $token = $token_info['data']['access_token'];

        $method = 'sms.template.delete';
        $url = 'https://openapi-fxg.jinritemai.com/sms/template/delete';

        $a1 = array(
            'code'=>'',
            'grant_type'=>$this->grant_type,
            'shop_id'=>$this->shop_id,
            //此处是接口特殊参数

            'sms_account'=>$_POST['sms_account'],
            'template_id'=>$_POST['template_id'],

        );
        $paramJson =  $this->get_param_json($a1);
        $sign = $this->get_sign($timestamp,$paramJson,$method);
        $params = array(
            'app_key'=>$appKey,
            'method'=>$method,
            'param_json'=>$paramJson,
            'timestamp'=>$timestamp,
            'v'=>'2',
            'sign'=>$sign,
            'sign_method'=> $this->sign_method,
            'access_token'=>$token,

        );
        $json_obj = Curl::callWebServer($url,$params,'POST',true,false);

        $data['paramJson'] = $paramJson;
        $data['sign'] = $sign;
        $data['token'] = $token;
        $data['result'] = $json_obj;
        return Res::ok($data);
    }
    /**
     * @author: oliver
     * @Description: 抖店短信 签名列表 接口
     * @Date: 2024-06-04 00:00:00
     */
    public function sms_sign_list(){
        $d['remark'] =  '我是走进了抖店短信';
        DB::table('xh_siyu_user_remark')->save($d);
        $appKey = $this->appKey;
        $appSecret = $this->appSecret;
        $timestamp =time();
        $v= '2';
        $token_info = $this->get_token($timestamp);
        $token = $token_info['data']['access_token'];

        $method = 'sms.sign.apply.list';
        $url = 'https://openapi-fxg.jinritemai.com/sms/sign/apply/list';

        $a1 = array(
            'code'=>'',
            'grant_type'=>$this->grant_type,
            'shop_id'=>$this->shop_id,
            //此处是接口特殊参数

            'sms_account'=>$_POST['sms_account'],

        );
        $paramJson =  $this->get_param_json($a1);
        $sign = $this->get_sign($timestamp,$paramJson,$method);
        $params = array(
            'app_key'=>$appKey,
            'method'=>$method,
            'param_json'=>$paramJson,
            'timestamp'=>$timestamp,
            'v'=>'2',
            'sign'=>$sign,
            'sign_method'=> $this->sign_method,
            'access_token'=>$token,

        );
        $json_obj = Curl::callWebServer($url,$params,'POST',true,false);

        $data['paramJson'] = $paramJson;
        $data['sign'] = $sign;
        $data['token'] = $token;
        $data['result'] = $json_obj;
        return Res::ok($data);
    }



        
        
        

}
